 <script>
import api from '@/api/ajax.js'

 	export default {
 		onLaunch: function(e) {
 			let pid = ''
 			if (e.query) {
 				if (e.query.pid) pid = e.query.pid
 				if (e.query.scene) {
 					pid = decodeURIComponent(e.query.scene);
 				}
 			}
			if(e?.referrerInfo?.extraData?.pid){
				pid = e?.referrerInfo?.extraData?.pid || ''
			}
 			uni.setStorageSync('pid', pid);

 			// #ifdef MP-WEIXIN
 			uni.requirePrivacyAuthorize({
 				success: () => {},
 				fail: () => {}
 			})
 			uni.setStorageSync('platform', 'MP-WEIXIN');

 			this.login_bind();
 			// uni.$on('restart_login', () => {
 			// 	this.login_bind();
 			// });
 			// #endif

 			// #ifdef APP-PLUS
 			this.login_app()

 			uni.onPushMessage(async (res) => {
 				if (res.type === 'click' || res.type === 'receive') {
 					let url = uni.getSystemInfoSync().platform == 'ios' ? JSON.parse(res.data.payload) :
 						res.data.payload
 					setTimeout(function() {
 						uni.navigateTo({
 							url,
 						})
 					}, 1000)
 					plus.push.clear()
 					plus.runtime.setBadgeNumber(0) //清除图标右上角数字
 				}
 			})

 			// uni.$on('restart_login', () => {
 			// 	this.login_app();
 			// });

 			uni.setStorageSync('platform', 'APP');
 			//锁定 666 为APP进来的用户
 			uni.setStorageSync('pid', 666);
 			// #endif
 		},
 		async onShow() {
 			// #ifdef APP-PLUS
 			this.version_update()
 			// #endif
			this.AppAuth()
 		},
 		onHide: function() {
 			//用户下线
 			this.$api.post('User/UserOffline');
 		},
 		methods: {
			async AppAuth(){
				let res = await api.post('Common/AppAuth')
				let IsOpenAuth = res?.data?.IsOpenAuth || 0
				uni.setStorageSync('IsOpenAuth',IsOpenAuth)
			},
 			async login_bind() {
 				let _this = this;
 				uni.login({
 					provider: 'weixin',
 					success: res => {
 						_this.wx_login(res.code);
 					},
 					fail: err => {
 						uni.showModal({
 							content: '登录失败',
 							showCancel: false
 						});
 					}
 				});
 			},
 			async login_app() {
 				let _this = this;
 				let token = uni.getStorageSync('xj_token');
 				if (!token) {
 					uni.reLaunch({
 						url: '/pages/start/isLogin'
 					})
 					return
 				}
 				uni.getLocation({
 					type: 'gcj02',
 					success: async function(r2) {
 						let r = await _this.$api.post('/Member/SaveOnlineData', {
 							Last_Lat: r2.latitude,
 							Last_Lng: r2.longitude
 						})
 					},
 				});
 				setTimeout(async () => {
 					let userID = uni.getStorageSync('user').ID;
 					let res = await this.$api.post('User/GetUserInfo', { ID: userID });
 					const user = res.data
 					const im = await this.$im.login(user.UserID, user.Name, user.HeadImgUrl)
 					this.$store.commit('login', true);
 					this.$store.commit('setUser', user);
 					uni.setStorageSync('xj_token', d.Token);
 					uni.setStorageSync('user', user);
 				}, 500)
 			},
 			async wx_login(code) {
 				uni.setStorageSync('loginKey', code)
 				let c_model = '';
 				let c_system = ''
 				uni.getSystemInfo({
 					success(res) {
 						c_model = res.model;
 						c_system = res.system;
 					}
 				});
 				let pid = uni.getStorageSync('pid');
 				let res = await this.$api.post('User/WXLogin', {
 					Code: code,
 					pid: pid
 				});

 				uni.setStorageSync('xj_token', res.data.Token);

 				let user = res.data.UserData;

 				uni.setStorageSync('user', user);
 				this.$store.commit('setUser', user);
 				const im = await this.$im.login(user.UserID, user.Name, user.HeadImgUrl)
					console.log(im,user.UserID, user.Name, user.HeadImgUrl,'imimimimimimimimimimim');

 				let _this = this;
 				uni.getLocation({
 					type: 'gcj02',
 					success: async function(r2) {
 						let r = await _this.$api.post('/Member/SaveOnlineData', {
 							Last_Lat: r2.latitude,
 							Last_Lng: r2.longitude
 						})
 					},
 				});
 			},
 			version_update() {
 				const info = uni.getSystemInfoSync()
 				uni.request({
 					url: 'https://medical.xiaojusmart.com/api/version/update',
 					data: {
 						appid: info.appId,
 						version: info.appVersion,
 						wgtVer: info.appWgtVersion,
 						platform: info.platform,
 						deviceId: info.deviceId,
 						brand: info.brand,
 						model: info.model,
 						system: info.system,
 					},
 					success: (res) => {
 						res = res.data
 						console.log('000000000000000====' + JSON.stringify(res));
 						if (res.type == 2) return
 						if (res.must == 2) {
 							//后台默认运行更新
 							this.update_version(res.url);
 							return;
 						}
 						setTimeout(() => {
 							uni.navigateTo({
 								url: '/pages/update?data=' + JSON.stringify(res)
 							});
 						}, 3000)
 					}
 				});
 			},
 			update_version(url) {
 				const _this = this
 				uni.downloadFile({
 					url,
 					success(res) {
 						if (res.statusCode == 200) {
 							_this.installBtn(res.tempFilePath);
 							return;
 						}
 					}
 				});
 			},
 			installBtn(path) {
 				plus.runtime.install(path, {
 						force: false
 					}, res => {
 						//更新完重启app
 						uni.showModal({
 							content: '更新完成，是否确认重启？',
 							showCancel: true,
 							success: res => {
 								if (res.confirm) {
 									plus.runtime.restart();
 								}
 							}
 						});
 					},
 					err => {
 						uni.$u.toast('更新失败')
 					});
 			}
 		}
 	}
 </script>

 <style lang="scss">
 	@import "uview-ui/index.scss";
 	@import 'static/css/common.scss';
 	/*每个页面公共css */
 </style>